package com.rocket.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rocket.R;

public class DialogUtil {

	private Context			mContext;

	private Dialog			mDialog;

	private LinearLayout	mMainLayout;	//

	private LinearLayout	titleLayout;
	private ImageView		icon;
	private TextView		titleText;

	private LinearLayout	contentLayout;

	private LinearLayout	bottomLayout;

	private float			scale;

	private int				width;

	public DialogUtil() {}

	public DialogUtil(Context context) {
		this.mContext = context;
		scale = mContext.getResources().getDisplayMetrics().density;
		width = mContext.getResources().getDisplayMetrics().widthPixels;
		mMainLayout = new LinearLayout(mContext);

		mMainLayout.setLayoutParams(new LinearLayout.LayoutParams(width - 40, LinearLayout.LayoutParams.WRAP_CONTENT));
		mMainLayout.setOrientation(LinearLayout.VERTICAL);

		titleLayout = new LinearLayout(mContext);
		titleLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0));
		titleLayout.setGravity(Gravity.CENTER_VERTICAL);
		titleLayout.setPadding(10, 0, 10, 0);
		titleLayout.setBackgroundResource(R.drawable.qqdialog_bg_top);
		icon = new ImageView(mContext);
		icon.setLayoutParams(new LinearLayout.LayoutParams(40,40));
		icon.setImageResource(R.drawable.dialog_default_icon);
		titleLayout.addView(icon);
		titleText = new TextView(mContext);
		titleText.setText("提示");
		titleText.setTextSize(16);
		titleText.setTextColor(Color.BLACK);
		titleText.setPadding(5, 0, 0, 0);
		titleLayout.addView(titleText);
		mMainLayout.addView(titleLayout);

		contentLayout = new LinearLayout(mContext);
		contentLayout.setLayoutParams(new LinearLayout.LayoutParams(width - 40, LinearLayout.LayoutParams.FILL_PARENT, 1));
		contentLayout.setGravity(Gravity.CENTER);
		contentLayout.setBackgroundResource(R.drawable.qqdialog_center);
		mMainLayout.addView(contentLayout);

		bottomLayout = new LinearLayout(mContext);
		bottomLayout.setLayoutParams(new LinearLayout.LayoutParams(width - 40,60, 0));
		bottomLayout.setGravity(Gravity.CENTER);
		bottomLayout.setPadding(5, 0, 5, 0);
		bottomLayout.setBackgroundResource(R.drawable.qqdialog_bottom);
		mMainLayout.addView(bottomLayout);

	}

	public void setIcon(int drawableId) {
		icon.setImageResource(drawableId);
	}

	public void setTitle(String text) {
		titleText.setText(text);
	}

	public void setTitle(int resId) {
		titleText.setText(resId);
	}

	public void show() {
		mDialog = new Dialog(mContext, R.style.quit_dialog);
		mDialog.setContentView(mMainLayout);
		int count = bottomLayout.getChildCount();
		if (count == 1) {
			Button button = (Button) bottomLayout.getChildAt(0);
			button.setLayoutParams(new LinearLayout.LayoutParams((width - 80) / 2, LinearLayout.LayoutParams.WRAP_CONTENT));
		}
		mDialog.show();
	}

	public void setMContentView(View contentView) {
		contentView.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		contentLayout.addView(contentView);
	}

	public void setMessage(String text) {
		TextView message = new TextView(mContext);
		message.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		message.setText(text);
		message.setPadding(15,10, 15, 10);
		message.setTextSize(16);
		message.setTextColor(Color.WHITE);
		contentLayout.addView(message);
	}

	public void setMessage(int resId) {
		TextView message = new TextView(mContext);
		message.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		message.setText(resId);
		message.setPadding(15, 10, 15, 10);
		message.setTextSize(16);
		message.setTextColor(Color.WHITE);
		contentLayout.addView(message);
	}

	public void setPositiveButton(String text, OnClickListener clickListener) {
		Button posButton = new Button(mContext);
		posButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
		posButton.setText(text);
		posButton.setGravity(Gravity.CENTER);
		if (clickListener == null) {
			posButton.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					mDialog.dismiss();
				}
			});
		} else {
			posButton.setOnClickListener(clickListener);
		}
		bottomLayout.addView(posButton);
	}

	public void setNeutralButton(String text, OnClickListener clickListener) {
		Button neuButton = new Button(mContext);
		neuButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
		neuButton.setText(text);
		neuButton.setGravity(Gravity.CENTER);
		if (clickListener == null) {
			neuButton.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					mDialog.dismiss();
				}
			});
		} else {
			neuButton.setOnClickListener(clickListener);
		}
		bottomLayout.addView(neuButton);
	}

	public void setNegativeButton(String text, OnClickListener clickListener) {
		Button negButton = new Button(mContext);
		negButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
		negButton.setText(text);
		negButton.setGravity(Gravity.CENTER);
		if (clickListener == null) {
			negButton.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					mDialog.dismiss();
				}
			});
		} else {
			negButton.setOnClickListener(clickListener);
		}
		bottomLayout.addView(negButton);
	}

	public void dismiss() {
		mDialog.dismiss();
	}
}
